package alpha2;

import java.nio.charset.Charset;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Camera extends Common{
	
	public static void get() throws Exception{
		System.out.println("Start GET /camera 获取所有相机 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/camera");

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
	}
	
	public static void add(String name, String url, String attachedParams) throws Exception {
        System.out.println("Start POST /camera 创建相机 ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost request = new HttpPost(HOST + "/camera");
        
        MultipartEntity reqEntity = new MultipartEntity();  
        reqEntity.addPart("name", new StringBody(name,Charset.forName("UTF-8")));
        reqEntity.addPart("url", new StringBody(url));
        reqEntity.addPart("attached_params", new StringBody(attachedParams,Charset.forName("UTF-8")));
        request.setEntity(reqEntity); 

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
    }
	
	public static void get(Integer cameraId) throws Exception{
		System.out.println("Start GET /camera/{camera_id} 获取单个相机 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/camera/" + cameraId);

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
	}
	
	
	public static void update(Integer cameraId, String name, String url, String attached_params) throws Exception {
        System.out.println("Start PUT /camera/{camera_id} 修改单个相机 ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPut request = new HttpPut(HOST + "/camera/" + cameraId);
        
        MultipartEntity reqEntity = new MultipartEntity();  
        reqEntity.addPart("name", new StringBody(name,Charset.forName("UTF-8")));
        reqEntity.addPart("url", new StringBody(url));
        reqEntity.addPart("attached_params", new StringBody(attached_params,Charset.forName("UTF-8")));
        request.setEntity(reqEntity); 

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
    }
	
	public static void delete(Integer cameraId) throws Exception{
		System.out.println("Start DELETE /camera/{camera_id} 删除单个相机 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpDelete request = new HttpDelete(HOST + "/camera/" + cameraId);

        //发起网络请求
        httpclient.execute(request);
        
	}
	
}
